package com.joolun.web.controller.scrm;

import java.util.List;

import com.joolun.weixin.service.IScrmJobUserService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.joolun.common.annotation.Log;
import com.joolun.common.core.controller.BaseController;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.common.enums.BusinessType;
import com.joolun.system.domain.ScrmJobUser;
import com.joolun.common.utils.poi.ExcelUtil;
import com.joolun.common.core.page.TableDataInfo;

/**
 * 教职人员Controller
 * 
 * @author jon
 * @date 2025-03-06
 */
@RestController
@RequestMapping("/scrm/teach")
public class ScrmJobUserController extends BaseController
{
    @Autowired
    private IScrmJobUserService scrmJobUserService;

    /**
     * 查询教职人员列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ScrmJobUser scrmJobUser)
    {
        startPage();
        List<ScrmJobUser> list = scrmJobUserService.selectScrmJobUserList(scrmJobUser);
        return getDataTable(list);
    }
    @GetMapping("/listno")
    public TableDataInfo listNo(ScrmJobUser scrmJobUser)
    {
        List<ScrmJobUser> list = scrmJobUserService.selectScrmJobUserList(scrmJobUser);
        return getDataTable(list);
    }
    /**
     * 导出教职人员列表
     */
    @PreAuthorize("@ss.hasPermi('scrm:teach:export')")
    @Log(title = "教职人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScrmJobUser scrmJobUser)
    {
        List<ScrmJobUser> list = scrmJobUserService.selectScrmJobUserList(scrmJobUser);
        ExcelUtil<ScrmJobUser> util = new ExcelUtil<ScrmJobUser>(ScrmJobUser.class);
        util.exportExcel(response, list, "教职人员数据");
    }

    /**
     * 获取教职人员详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(scrmJobUserService.selectScrmJobUserById(id));
    }

    /**
     * 新增教职人员
     */
    @PreAuthorize("@ss.hasPermi('scrm:teach:add')")
    @Log(title = "教职人员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScrmJobUser scrmJobUser)
    {
        scrmJobUser.setCreateBy(getUsername());
        return toAjax(scrmJobUserService.insertScrmJobUser(scrmJobUser));
    }

    /**
     * 修改教职人员
     */
    @PreAuthorize("@ss.hasPermi('scrm:teach:edit')")
    @Log(title = "教职人员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScrmJobUser scrmJobUser)
    {
        // 效验兜底二维码是否只有一个
        if("1".equals(scrmJobUser.getCodeDefault())){
            List<ScrmJobUser> list = scrmJobUserService.selectIsJobUserList(scrmJobUser);
            if(list.size() == 1){
                // 效验是否只有一个兜底二维码
                if(scrmJobUser.getId() != list.get(0).getId()){
                    return AjaxResult.error("只能设置一个兜底接量二维码");
                }
            }else if(list.size() > 1){
                return AjaxResult.error("只能设置一个兜底接量二维码");
            }
        }
        scrmJobUser.setCreateBy(getUsername());
        return toAjax(scrmJobUserService.updateScrmJobUser(scrmJobUser));
    }

    /**
     * 删除教职人员
     */
    @PreAuthorize("@ss.hasPermi('scrm:teach:remove')")
    @Log(title = "教职人员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scrmJobUserService.deleteScrmJobUserByIds(ids));
    }
}
